package com.tanhua.app.controller;

import com.alibaba.fastjson.JSON;
import com.tanhua.app.controllerservice.InteractionService;
import com.tanhua.domain.db.User;
import com.tanhua.domain.vo.FreeZing;
import com.tanhua.domain.vo.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
public class InteractionController {
    @Autowired
    private InteractionService interactionService;

    @PostMapping(name = "视频点赞",value = "/smallVideos/{id}/like")
    public ResponseEntity like(@PathVariable("id") String videoId) throws IOException {
        return interactionService.like(videoId);
    }

    @PostMapping(name = "视频点赞 - 取消",value = "/smallVideos/{id}/dislike")
    public ResponseEntity unLike(@PathVariable("id") String videoId) throws IOException {
        return interactionService.unLike(videoId);
    }
    @GetMapping(name = "评论列表",value = "/smallVideos/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String commentsId ,Integer page,Integer pagesize) throws IOException {
        return interactionService.pllb(commentsId,page,pagesize);
    }

    @PostMapping(name = "评论发布", value = "/smallVideos/{id}/comments")
    public ResponseEntity comment(@PathVariable("id") String commentsId, @RequestBody Map<String, String> map) throws IOException {
        User user = UserHolder.getUser();
        System.out.println(user);
        if (stringRedisTemplate.hasKey("freeZing_" + user.getId())) {
            String freeZingJson = stringRedisTemplate.opsForValue().get("freeZing_" + user.getId());
            System.out.println(freeZingJson);
            FreeZing freeZing = JSON.parseObject(freeZingJson, FreeZing.class);
            if (freeZing.getFreezingRange() == 2) {
                return ResponseEntity.status(403).body("你没有该权限");
            }
        }
        return interactionService.pinglun(commentsId, map.get("comment"));
    }
}
